// 存储localStorage
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    localStorage.setItem(name, content);
};

// 获取localStorage
export const getStore = name => {
    if (!name) return;
    return localStorage.getItem(name);
};

/**
 * 表单校验
 * formData 表单数据
 * formRules 表单校验规则
 * 校验是否必填 自定义正则
 */
export const validate = (formData,formRules) => {
    if(!formRules || Object.keys(formRules).length === 0){
        throw { message : '校验规则不能为空'}
    }
    let rules = Object.entries(formRules);
    rules.forEach((item)=>{
        let key = item[0];//key
        let value = item[1];//value
        if(value.length > 0){
            let formItemData = formData[key];//获取表单数据
            value.forEach(v=>{
                if(v.required && !formItemData){//是否必填
                    throw {message:v.message ? v.message : key+'不能为空'}
                }
                if(v.pattern && !v.pattern.test(formItemData)){//校验自定义正则表达式
                    throw {message:v.message ? v.message : key+'格式不正确'}
                }
            })
        }
    });
    return true;
};